<!--
 * @Author: your name
 * @Date: 2021-10-18 09:54:05
 * @LastEditTime: 2021-11-07 21:20:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wisdom-education-admin-front-v1.0.2-master-master\src\views\education\examroom.vue
-->
<template>
  <div>
    <!-- 搜索 添加 -->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input
          placeholder="请输入考场名称"
          v-model="keywordRoom"
          clearable
          @keyup.native="keyup()"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getExamRoomList"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="2">
        <el-button
          icon="el-icon-circle-plus-outline"
          type="primary"
          plain
          @click="addExamRoomVisible = true"
          >添加考场</el-button
        >
      </el-col>
    </el-row>
    <br />

    <!-- 考场列表 -->
    <el-table :data="roomList" border stripe style="width: 100%">
      <el-table-column type="index" width="50"> </el-table-column>

      <el-table-column label="考场名称" align="center" prop="name">
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createDate">
      </el-table-column>

      <!-- <el-table-column label="考场总人数" align="center" prop="studentCount">
      </el-table-column> -->

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!--<el-button
            size="small"
            type="text"
            icon="el-icon-s-custom"
            @click="examRoomStu(scope.row)"
            >考生管理</el-button
          > -->
          <el-button
            size="small"
            icon="el-icon-delete"
            type="text"
            @click="deleteRoom(scope.row)"
          >
            删除考场
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 管理考生对话框 -->
    <el-dialog
      width="80%"
      title="考场--考生管理"
      :visible.sync="roomStuVisible"
      :close-on-click-modal="false"
    >
      <!-- 搜索 添加考生 批量删除 -->
      <el-row>
        <el-col :span="16">
          <el-form>
            <el-row>
              <el-col :span="6">
                <el-form-item>
                  <el-select
                    v-model="gradeInfoId"
                    filterable
                    clearable
                    placeholder="请选择年级"
                  >
                    <el-option
                      v-for="item in gradeInfoList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item>
                  <el-input
                    v-model="keyWordStu"
                    placeholder="输入学生名字"
                    type="text"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item>
                  <el-button
                    type="success"
                    icon="el-icon-search"
                    @click="searcStudentExamroom()"
                    >搜索</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="openStudentdialog"
            >往考场中添加考生</el-button
          >
        </el-col>

        <el-button
          type="danger"
          v-if="selectNowExamRoomStduentIds.length > 0"
          @click="delSelectStduent"
          >批量删除</el-button
        >
      </el-row>
      <br />

      <!-- 当前考场的考生列表 -->
      <el-table
        :data="studentList"
        border
        stripe
        @selection-change="selectNowExamRoomStduent"
      >
        <el-table-column
          type="selection"
          align="center"
          width="50"
        ></el-table-column>

        <el-table-column
          label="姓名"
          align="center"
          prop="studentName"
        ></el-table-column>

        <el-table-column label="年级" align="center" prop="gradeInfoName">
          <template slot-scope="scope">
            <el-tag size="small">{{ scope.row.gradeInfoName }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="small" icon="el-icon-delete" type="text"
              >移除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部 -->
      <span slot="footer">
        <el-button @click="roomStuVisible = false">取 消</el-button>
        <el-button type="primary">完成管理</el-button>
      </span>
    </el-dialog>

    <!-- 添加考场对话框 -->
    <el-dialog
      title="新增考场"
      :visible.sync="addExamRoomVisible"
      :close-on-click-modal="false"
      width="30%"
      @close="addRoomDialogClosed"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="考场名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addExamRoomVisible = false">取 消</el-button>
        <el-button type="primary" @click="addExamRoom">确 定</el-button>
      </span>
    </el-dialog>

    <!--学员对话框-->
    <el-dialog
      width="50%"
      title="添加考生"
      :visible.sync="addStuVisible"
      @close="addStuListDialogClosed"
      :close-on-click-modal="false"
      append-to-body
    >
      <!--选择年级  输入考生姓名   搜索学员-->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-select
            v-model="query.gradeInfoId"
            filterable
            clearable
            placeholder="请选择年级"
          >
            <el-option
              v-for="item in gradeInfoList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>

        <el-col :span="6">
          <el-input
            v-model="query.keyWord"
            placeholder="请输入考生姓名"
          ></el-input>
        </el-col>

        <el-button
          type="primary"
          v-if="hasPermission(['system:student:list'])"
          @click="searchStudent"
          >搜索</el-button
        >
      </el-row>
      <br />

      <!-- 学员列表 -->
      <el-table
        :data="infoStudentList"
        @selection-change="handleSelectionChange"
        border
        stripe
      >
        <el-table-column type="selection" align="center" width="50">
        </el-table-column>

        <el-table-column property="name" label="姓名"></el-table-column>

        <el-table-column label="年级" align="center" prop="grade">
          <template slot-scope="scope">
            <el-tag size="small">{{ scope.row.gradeName }}</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <br />

      <!-- 学员的底部 -->
      <span slot="footer">
        <el-button @click="addStuVisible = false">取 消</el-button>
        <el-button type="primary" @click="getStuToRoom()">完成添加</el-button>
      </span>

      <!-- 学员分页 -->
      <el-pagination
        background
        @size-change="AllStusizeChangeHandler"
        @current-change="AllStucurrentChangeHandler"
        :current-page="AllStucurrentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="AllStupageSize"
        :total="AllStutotalCount"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-dialog>

    <!-- 考场分页 -->
    <el-pagination
      background
      @size-change="RoomhandleSizeChange"
      @current-change="RoomhandleCurrentChange"
      :current-page="RoompageNumber"
      :page-sizes="[5, 10, 30, 40]"
      :page-size="RoompageSize"
      :total="RoomtotalCount"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getGradeInfoList } from "@/api/gradeInfo";
export default {
  data() {
    return {
      roomList: [],
      keywordRoom: "",
      form: {
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输考场名称", trigger: "blur" }],
      },

      checkedTestTaskIds: [],
      RoompageSize: 10,
      RoompageNumber: 1, //当前页
      RoomtotalCount: 0, //总数
      RoomtotalPerson: [],
      Roomtotal: 0,

      //考生管理对话框开关
      roomStuVisible: false,
      //添加考场对话框开关
      addExamRoomVisible: false,
      //添加考生对话框开关
      addStuVisible: false,
      //当前考场的考生
      studentList: [],
      //所有学生列表(未添加进考场的考生)
      infoStudentList: [],
      input: "",
      //存储年级信息
      gradeInfoId: "",
      //年级下拉框列表
      gradeInfoList: [],
      //输入学生姓名输入框
      keyWordStu: "",
      query: {
        keyWordStu: "",
        gradeInfoId: "",
      },
      //往考场中添加考生数组
      multipleSelection: [],
      //学员分页
      AllStucurrentPage: 1,
      AllStutotalCount: 0,
      AllStupageSize: 5,
      ids: [],
      studentExamRoomId: 0, //本考场Id
      selectNowExamRoomStduentIds: [],
    };
  },
  created() {
    this.getExamRoomList();
  },
  mounted() {
    // this.getInfoStudentList();
    getGradeInfoList({}).then((response) => {
      this.gradeInfoList = response.data.data.dataList;
    });
  },

  watch: {},

  methods: {
    keyup() {
      if (this.judgeNull(this.keyword)) {
        this.getExamTaskList();
      }
    },
    //判空
    judgeNull(str) {
      if (str == "") return true;
      var regu = "^[ ]+$";
      var re = new RegExp(regu);
      return re.test(str);
    },
    openStudentdialog() {
      this.addStuVisible = true;
      this.getInfoStudentList();
    },
    //考场列表
    getExamRoomList() {
      this.axios
        .get(this.$http.httpUrl(`/system/examRoom/getExamRoomList`), {
          params: {
            pageSize: this.RoompageSize,
            pageNumber: this.RoompageNumber,
            name: this.keywordRoom,
          },
        })
        .then((response) => {
          // console.log(response);
          this.roomList = response.data.data.dataList;
          this.RoomtotalCount = response.data.data.total;
          // console.log(this.totalPerson);
        });
    },

    delSelectStduent() {
      let ids = "";
      let arr = [];
      arr = this.selectNowExamRoomStduentIds;
      arr.forEach((value, index) => {
        ids += value.id + ",";
      });
      this.axios
        .get(this.$http.httpUrl(`/examroomstudent/delectStudentAllById`), {
          params: {
            id: ids,
          },
        })
        .then((response) => {
          this.$message({
            type: "success",
            message: "成功移除" + response.data + "个学生",
          });
          this.searcStudentExamroom();
        });
    },

    searcStudentExamroom() {
      this.axios
        .get(this.$http.httpUrl(`examroomstudent/getExamRoomStudentList`), {
          params: {
            examRoomId: this.studentExamRoomId,
            // pageSize: this.RoompageSize,
            // pageNumber: this.RoompageNumber,

            studentName: this.keyWordStu,
            gradeInfoName: this.gradeInfoId,
          },
        })
        .then((response) => {
          // console.log("response");
          // console.log(response);
          this.studentList = response.data.data.dataList;
          // this.RoomtotalCount = response.data.data.total;
          // console.log(this.totalPerson);
        });
    },
    // 获取考场学生
    getExamRoomStudentList(item) {
      this.studentExamRoomId = item.id;
      const that = this;
      this.axios
        .get(this.$http.httpUrl(`examroomstudent/getExamRoomStudentList`), {
          params: {
            examRoomId: item.id,
            // pageSize: 5,
            // pageNumber: 1,
          },
        })
        .then((response) => {
          // console.log("response");
          // console.log(response);
          that.studentList = response.data.data.dataList;
          // this.RoomtotalCount = response.data.data.total;
          // console.log(this.totalPerson);
        });
    },
    //监听添加考场对话框的关闭事件
    addRoomDialogClosed() {
      //重置表单验证提示
      this.$refs.form.resetFields();
    },
    addExamRoom() {
      var that = this;
      that.$refs.form.validate((valid) => {
        if (!valid) return;
        that.axios
          .get(that.$http.httpUrl(`/system/examRoom/addExamRoom`), {
            params: {
              name: that.form.name,
            },
          })
          .then((response) => {
            if (response.data.code === 1) {
              that.$message({
                type: "success",
                message: response.data.message,
              });
            }
            that.addExamRoomVisible = false;
          });
      });
      that.getExamRoomList();
    },
    // 删除考场
    deleteRoom(item) {
      this.$confirm(
        "此操作将删除【" +
          item.name +
          "】考场，并清空在系统中的所有数据,删除后不可恢复,是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.axios
          .get(
            this.$http.httpUrl(
              `/system/examRoom/deleteExamRoomById?id=` + item.id
            )
          )
          .then((response) => {
            if (response.data.code === 1) {
              this.$message.success("删除成功");
              this.getExamRoomList();
            }
          });
      });
    },
    //当前获取当前考场学生
    selectNowExamRoomStduent(val) {
      this.selectNowExamRoomStduentIds = val;
    },
    //管理考生
    examRoomStu(item) {
      this.roomStuVisible = true;
      this.getExamRoomStudentList(item);
      // console.log("room_id:", item);
    },
    RoomhandleSizeChange(val) {
      this.RoompageNumber = 1;
      this.RoompageSize = val;
      this.getExamRoomList();
    },
    RoomhandleCurrentChange(val) {
      this.RoompageNumber = val;
      this.getExamRoomList();
    },
    RoomhandleSelectionChange(val) {
      this.checkedTestTaskIds = val;
      // console.log(val);
    },

    //获取学员列表
    getInfoStudentList() {
      this.axios
        .get(this.$http.httpUrl("/system/student/selectNoTestRoomList"), {
          params: {
            pageNumber: this.AllStucurrentPage,
            pageSize: this.AllStupageSize,
            name: this.query.keyWord,
            gradeInfoId: this.query.gradeInfoId,
            roomId: this.studentExamRoomId,
          },
        })
        .then((response) => {
          this.infoStudentList = response.data.data.dataList;
          this.AllStutotalCount = response.data.data.total;
        });
    },
    // 添加学生到考场
    getStuToRoom() {
      const that = this;
      let room = {
        id: this.studentExamRoomId,
      };
      const roomStudent = {
        roomId: this.studentExamRoomId, // 获取考场id
        students: this.multipleSelection, // 学生列表
      };
      this.axios
        .post(
          this.$http.httpUrl("/examroomstudent/addSelectStudent"),
          roomStudent
        )
        .then((response) => {
          if (response.data.code === 1) {
            this.addStuVisible = false;
            this.$message.success("添加成功");
          }
        });
      // console.log(room.id);

      // this.$message.success("添加成功");

      that.getExamRoomStudentList(room);
    },
    handleSelectionChange(val) {
      // console.log(val);
      this.multipleSelection = val;
      // console.log()
    },
    //学员对话框关闭事件
    addStuListDialogClosed() {
      this.query = "";
      this.getInfoStudentList();
    },
    //学员分页
    AllStusizeChangeHandler(val) {
      this.AllStupageSize = val;
      this.AllStucurrentPage = 1;
      this.getInfoStudentList();
    },
    AllStucurrentChangeHandler(val) {
      this.AllStucurrentPage = val;
      this.getInfoStudentList();
    },
    //搜索学员
    searchStudent() {
      this.currentPage = 1;
      this.getInfoStudentList();
    },
  },
};
</script>

<style lang="less" scoped>
</style>
